﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Black_Jack
{
    public partial class Form1 : Form
    {
        private readonly Analizer analizer = new Analizer();
        private Socket NewClient;
        private int Stavka = 1;
        private int Stek = 100;
        private Thread ThServ;
        private int asd;
        private Socket clientSok;
        public int fl;
        public int flag;
        private IPEndPoint ipEndPt1;
        private string mes;
        private int ot;
        private string otvet;
        private int pt1 = 12345, s1;
        private Socket sListen;
        private string stProtivnik;
        public int us;

        public Form1()
        {
            InitializeComponent();
        }

        private void Listen()
        {
            sListen.Listen(10);
            clientSok = sListen.Accept();
            var t = new Thread(listserv);
            t.Start();
        }


        private void listcl()
        {
            var play = new int[9];
            int i, s2 = 0, d = 0;
            while (true)
            {
                mes = NewClient.Receive();
                NewClient.Send(MyChipsTextBox.Text);
                stProtivnik = NewClient.Receive();
                NewClient.Send(MyBetTextBox.Text);
                i = 0;
                while (i < 9)
                {
                    asd = Convert.ToInt32(NewClient.Receive());
                    NewClient.Send(asd.ToString());
                    play[i] = asd;
                    i++;
                }

                pictureBox8.Image = imageList1.Images[play[7]];
                pictureBox9.Image = imageList1.Images[play[8]];

                if (fl == 0)
                {
                    int a, b;
                    a = Convert.ToInt32(MyBetTextBox.Text);
                    b = Convert.ToInt32(stProtivnik);
                    if ((a == b) && (a != 0) && (s2 == 0) && (flag == 1) && (b != 1000))
                    {
                        pictureBox3.Visible = true;
                        pictureBox4.Visible = true;
                        pictureBox5.Visible = true;
                        pictureBox3.Image = imageList1.Images[play[2]];
                        pictureBox4.Image = imageList1.Images[play[3]];
                        pictureBox5.Image = imageList1.Images[play[4]];
                        BankTextBox.Text = Convert.ToString(a + b);
                        MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) - a);
                        flag = 0;
                        s2 = 1;
                    }
                    a = Convert.ToInt32(MyBetTextBox.Text);
                    b = Convert.ToInt32(ClientBetTextBox.Text);
                    if ((a == b) && (a != 0) && (s2 == 1) && (flag == 1) && (b != 1000))
                    {
                        pictureBox6.Visible = true;
                        pictureBox6.Image = imageList1.Images[play[5]];
                        BankTextBox.Text = Convert.ToString(a + b + Convert.ToUInt32(BankTextBox.Text));
                        MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) - a);
                        flag = 0;
                        s2 = 2;
                    }
                    a = Convert.ToInt32(MyBetTextBox.Text);
                    b = Convert.ToInt32(ClientBetTextBox.Text);
                    if ((a == b) && (a != 0) && (s2 == 2) && (flag == 1) && (b != 1000))
                    {
                        pictureBox7.Visible = true;
                        pictureBox7.Image = imageList1.Images[play[6]];
                        BankTextBox.Text = Convert.ToString(a + b + Convert.ToUInt32(BankTextBox.Text));
                        MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) - a);
                        flag = 0;
                        s2 = 3;
                    }
                    a = Convert.ToInt32(MyBetTextBox.Text);
                    b = Convert.ToInt32(ClientBetTextBox.Text);
                    if ((a == b) && (a != 0) && (s2 == 3) && (flag == 1) && (b != 1000))
                    {
                        pictureBox1.Visible = true;
                        pictureBox2.Visible = true;
                        pictureBox1.Image = imageList1.Images[play[0]];
                        pictureBox2.Image = imageList1.Images[play[1]];
                        BankTextBox.Text = Convert.ToString(a + b + Convert.ToUInt32(BankTextBox.Text));
                        MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) - a);
                        s2 = 4;
                    }
                    if (((s2 == 4) && (flag == 1)) || (b == 1000))
                    {
                        flag = 0;
                        d = Convert.ToInt32(NewClient.Receive());
                        if ((d == 2) || (b == 1000))
                        {
                            MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) + Convert.ToInt32(BankTextBox.Text));
                        }
                        if (d == 0)
                        {
                            MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) + (Convert.ToInt32(BankTextBox.Text)/2));
                        }
                    }
                }
            }
        }

        private void listserv()
        {
            int i;

            while (true)
            {
                clientSok.Send(MyChipsTextBox.Text);
                otvet = clientSok.Receive();
                clientSok.Send(MyBetTextBox.Text);
                stProtivnik = clientSok.Receive();
                i = 0;
                while (i < 9)
                {
                    clientSok.Send(Convert.ToString(analizer.deckCard[i]));
                    clientSok.Receive();
                    i++;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int k;

            MyChipsTextBox.Text = Convert.ToString(Stek);
            MyBetTextBox.Text = "0";
            ClientBetTextBox.Text = "0";
            BetValueTextBox.Text = "1";
            BankTextBox.Text = "0";
            ipEndPt1 = new IPEndPoint(IPAddress.Loopback, pt1);
        }

        public void button4_Click(object sender, EventArgs e)
        {
            fl = 0;
            if (us == 0)
            {
                sListen = new Socket(AddressFamily.InterNetwork,
                                     SocketType.Stream, ProtocolType.Tcp);

                sListen.Bind(ipEndPt1);
                ThServ = new Thread(Listen);
                ThServ.Start();
            }
            us = 1;
            timer2.Start();
            analizer.karta();
            analizer.CalculateCombinationValueForServer();
            analizer.CalculateCombinationValueForClient();
            ot = analizer.CalculateResult();
            pictureBox1.Image = imageList1.Images[analizer.deckCard[0]];
            pictureBox2.Image = imageList1.Images[analizer.deckCard[1]];
        }

        public void button5_Click(object sender, EventArgs e)
        {
            fl = 0;

            if (us == 0)
            {
                try
                {
                    NewClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
                catch
                {
                    MessageBox.Show("ppc");
                }
                NewClient.Connect(ipEndPt1);
                var t = new Thread(listcl);
                t.Start();
            }
            us = 2;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ClientChipsTextBox.Text = mes;
            if ((stProtivnik == "1000") && (fl == 0))
            {
                fl = 1;
                MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) + Convert.ToInt32(BankTextBox.Text));
            }
            else if (fl == 0)
            {
                if (stProtivnik == null)
                {
                    ClientBetTextBox.Text = "0";
                }
                else
                {
                    ClientBetTextBox.Text = stProtivnik;
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            ClientChipsTextBox.Text = otvet;
            if ((stProtivnik == "1000") && (fl == 0))
            {
                fl = 1;
                MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) + Convert.ToInt32(BankTextBox.Text));
            }
            else if (fl == 0)
            {
                if (stProtivnik == null)
                {
                    ClientBetTextBox.Text = "0";
                }
                else
                {
                    ClientBetTextBox.Text = stProtivnik;
                }

                int myBet, clientBet, myChips;

                myBet = Convert.ToInt32(MyBetTextBox.Text);
                clientBet = Convert.ToInt32(ClientBetTextBox.Text);
                
                if ((myBet == clientBet) && (clientBet != 0) && (s1 == 0) && (flag == 1) && (clientBet != 1000))
                {
                    pictureBox3.Visible = true;
                    pictureBox4.Visible = true;
                    pictureBox5.Visible = true;
                    pictureBox3.Image = imageList1.Images[analizer.deckCard[2]];
                    pictureBox4.Image = imageList1.Images[analizer.deckCard[3]];
                    pictureBox5.Image = imageList1.Images[analizer.deckCard[4]];
                    myChips = Convert.ToInt32(MyChipsTextBox.Text);
                    BankTextBox.Text = Convert.ToString(myBet + clientBet);
                    MyChipsTextBox.Text = Convert.ToString(myChips - clientBet);
                    flag = 0;
                    s1 = 1;
                }
                myBet = Convert.ToInt32(MyBetTextBox.Text);
                clientBet = Convert.ToInt32(ClientBetTextBox.Text);
                if ((myBet == clientBet) && (clientBet != 0) && (s1 == 1) && (flag == 1) && (clientBet != 1000))
                {
                    pictureBox6.Visible = true;
                    pictureBox6.Image = imageList1.Images[analizer.deckCard[5]];
                    BankTextBox.Text = Convert.ToString(myBet + clientBet + Convert.ToUInt32(BankTextBox.Text));
                    MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) - myBet);
                    flag = 0;
                    s1 = 2;
                }
                myBet = Convert.ToInt32(MyBetTextBox.Text);
                clientBet = Convert.ToInt32(ClientBetTextBox.Text);
                if ((myBet == clientBet) && (clientBet != 0) && (s1 == 2) && (flag == 1) && (clientBet != 1000))
                {
                    pictureBox7.Visible = true;
                    pictureBox7.Image = imageList1.Images[analizer.deckCard[6]];
                    BankTextBox.Text = Convert.ToString(myBet + clientBet + Convert.ToUInt32(BankTextBox.Text));
                    MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) - myBet);
                    flag = 0;
                    s1 = 3;
                }
                myBet = Convert.ToInt32(MyBetTextBox.Text);
                clientBet = Convert.ToInt32(ClientBetTextBox.Text);
                if ((myBet == clientBet) && (clientBet != 0) && (s1 == 3) && (flag == 1) && (clientBet != 1000))
                {
                    pictureBox8.Visible = true;
                    pictureBox9.Visible = true;
                    pictureBox8.Image = imageList1.Images[analizer.deckCard[7]];
                    pictureBox9.Image = imageList1.Images[analizer.deckCard[8]];
                    BankTextBox.Text = Convert.ToString(myBet + clientBet + Convert.ToUInt32(BankTextBox.Text));
                    MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) - myBet);
                    s1 = 4;
                }
                if ((s1 == 4) && (flag == 1))
                {
                    flag = 0;
                    clientSok.Send(Convert.ToString(analizer.CalculateResult()));

                    int result = analizer.CalculateResult();
                    
                    if (result == 1)
                    {
                        MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) + Convert.ToInt32(BankTextBox.Text));
                    }
                    if (result == 0)
                    {
                        MyChipsTextBox.Text = Convert.ToString(Convert.ToInt32(MyChipsTextBox.Text) + (Convert.ToInt32(BankTextBox.Text)/2));
                    }
                }
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            BetValueTextBox.Text = BetValueTrackBar.Value.ToString();
            BetValueTrackBar.Maximum = Convert.ToInt32(MyChipsTextBox.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
        }

        private void label5_Click(object sender, EventArgs e)
        {
        }

        private void button9_Click(object sender, EventArgs e)
        {
            flag = 1;
            MyBetTextBox.Text = BetValueTextBox.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (us == 1)
            {
                button4_Click(sender, e);

                pictureBox3.Visible = false;
                pictureBox4.Visible = false;
                pictureBox5.Visible = false;
                pictureBox6.Visible = false;
                pictureBox7.Visible = false;
                pictureBox8.Visible = false;
                pictureBox9.Visible = false;
            }
            if (us == 2)
            {
                button5_Click(sender, e);
                pictureBox3.Visible = false;
                pictureBox4.Visible = false;
                pictureBox5.Visible = false;
                pictureBox6.Visible = false;
                pictureBox7.Visible = false;
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
            }
            MyBetTextBox.Text = "0";
            ClientBetTextBox.Text = "0";
            BetValueTextBox.Text = "1";
            BankTextBox.Text = "0";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            MyBetTextBox.Text = "1000";
        }
    }

    public static class SocketExtensions
    {
        public static string Receive(this Socket socket)
        {
            var buffer = new byte[1024];
            int count = socket.Receive(buffer);
            return Encoding.Default.GetString(buffer, 0, count);
        }

        public static void Send(this Socket socket, string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException();
            }
            socket.Send(Encoding.Default.GetBytes(message));
        }
    }
}